/*
========================================================================
 Name        : SingaporeBusServicesListBoxView.h
 Author      : 
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
#ifndef __IRISQUERY__
#define __IRISQUERY__

#include "HTTPUtils.h"
#include "CActiveHelper.h"
#include <euserhl.h>
#include "ActiveTarget.h"
#include "imageconversion.h"
#include <http.h>
#include <bitmaptransforms.h>
#include <akndialog.h>
#include <ArrivalTimeQuery.h>
#include <es_sock.h>

class CIrisSession;

struct CIrisQuery : public CArrivalTimeQuery
{
	CONSTRUCTORS_MAY_LEAVE
	typedef scalp::CFunction<void(CIrisQuery*, const TDesC8&, const TDesC8&, TServiceRequestStatus, const TDesC&, const TDesC8&, const TDesC8&) > TCallback;
	HBufC8* iServiceName;
	HBufC8* iStationNumber;
	TBool iStationInterchange;
	TServiceRequestStatus iRequestStatus;
	RHTTPTransaction iServiceTransaction;	
	CHTTPTransactionTarget* iServiceTransactionTarget;
	CHTTPTransactionSource* iServiceTransactionSource;
	LManagedPtr< TCallback > iCallback;
	CIrisSession* iSession;
	CIrisQuery(CIrisSession* aSession, const TDesC8& aServiceName,  const TDesC8& aStationNumber, TBool aStationInterchange, TCallback* aCallback);
	void SubmitL();
	void Cancel();
	~CIrisQuery();
};


class CIrisSession : public MActiveHelperCallback<RTimer*>
	{
	
	RArray<CIrisQuery*> iIrisQueries;
	friend class CIrisQuery;
public:
	// constructors and destructor
	CIrisSession();
	static CIrisSession* NewL();
	static CIrisSession* NewLC();        
	void ConstructL();
	virtual ~CIrisSession();

	RHTTPSession iHTTPSession;
	void SetConnectionInfoL(const RConnection& aConnection, const RSocketServ& aSocketServ);
	void CloseConnectionL();


	RTimer iIrisTimeoutTimer, iCrowdIrisTimeoutTimer;
	CActiveHelper<RTimer*>* iIrisTimeoutTimerHelper, * iCrowdIrisTimeoutTimerHelper;
	void MActiveHelperCallbackRunL(CActiveHelper<RTimer*>* aActiveHelper, RTimer* aTimer);
	TInt MActiveHelperCallbackRunError(CActiveHelper<RTimer*>* aActiveHelper, RTimer* aTimer, TInt aError);
	void MActiveHelperCallbackDoCancel(CActiveHelper<RTimer*>* aActiveHelper, RTimer* aTimer);
	TBool AllRequestsCompleted() const;
	
	void NotifyConnected();
	void SubmitL(CIrisQuery* aQuery);
	void Cancel(CIrisQuery* aQuery);
	void HandleResourceChange(TInt aType);
	
	CAknDialog* iIrisCaptchaDialog;
	
	void SubmitIrisApiRequestL(CIrisQuery* aQuery);
	void ReceivedIrisApiAnswerL(CIrisQuery* aQuery, RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	TInt IrisApiTransactionError(CIrisQuery* aQuery, TInt aError, RHTTPTransaction aTransaction);
	void SubmitIrisMobresultRequestL(CIrisQuery* aQuery);
	void SubmitIrisTokenRequestL();
	void SubmitCrowdIrisTokenRequestL();
	void SubmitCrowdIrisTokenUpdateRequestL();
	void ReceivedIrisMobresultAnswerL(CIrisQuery* aQuery, RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void ReceivedIrisTokenAnswerL(RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void ReceivedCrowdIrisTokenAnswerL(RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void ReceivedCrowdIrisTokenUpdateAnswerL(RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void ReceivedSmrtBusArrivalSearchNoHeaderAnswerBodyDataL(CIrisQuery* aQuery, RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf, TInt aPartSize);
	void ReceivedIrisCaptchaAnswerL(RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void CancelAllIrisRequestsL();
	void IrisCaptchaImageDecoded();
	void IrisCaptchaImageScaled();
	TInt IrisTransactionError(CIrisQuery* aQuery, TInt aError, RHTTPTransaction aTransaction);
	TInt CrowdIrisTransactionError(CIrisQuery* aQuery, TInt aError, RHTTPTransaction aTransaction);
	TBool iIrisInitalizationCycle;
	RHTTPTransaction iIrisTokenTransaction, iCrowdIrisTokenTransaction, iIrisCaptchaTransaction, iCrowdIrisTokenUpdateTransaction;
	LManagedPtr<CHTTPTransactionTarget> iIrisTokenTransactionTarget, iCrowdIrisTokenTransactionTarget, iIrisCaptchaTransactionTarget, iCrowdIrisTokenUpdateTransactionTarget;
	HBufC8* iIrisRlStickyKey;
	HBufC8* iIrisCaptchaActionPage;
	HBufC8* iIrisToken;
	TBool iCrowdIrisToken;
	TInt iCrowdIrisTokenId;
	CIrisQuery* iIrisInitializationPilotService;
	TInt iIrisCaptcha;
	TInt iIrisStatusCode;
	TTime iLastFailureIrisApi;
	TTime iLastFailureCrowdIrisToken;
	

	
	LManagedPtr<CImageDecoder> iIrisCaptchaDecoder;
	LManagedPtr<CFbsBitmap> iIrisCaptchaBitmap;
	LManagedPtr<CBitmapScaler> iIrisCaptchaBitmapScaler;
	LManagedPtr<CHTTPTransactionSource> iIrisCaptchaTransactionSource;
	// targets need to be bottom to be first to be deleted (they trigger cancel 
	LManagedPtr<scalp::CActiveTarget> iIrisCaptchaDecoderTarget, iIrisCaptchaBitmapScalerTarget;
	
	};

#endif // __IRISQUERY__
